/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeNodes(ListNode* head) {
        ListNode* fast = head->next, * slow = head;
        while (fast)
        {
            int val = 0;
            while (fast->val != 0)
            {
                val += fast->val;
                ListNode* next = fast->next;
                delete fast;
                fast = next;
            }
            slow->next = fast;
            slow = slow->next;
            slow->val = val;
            fast = fast->next;
        }
        ListNode* newHead = head->next;
        delete head;
        return newHead;
    }
};